/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.life.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.modules.life.entity.OrganizeLifeType;
import com.fosung.frame.modules.life.service.OrganizeLifeTypeService;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * 组织生活分类管理Controller
 * @author ygl
 * @version 2017-08-30
 */
@Controller
@RequestMapping(value = "${adminPath}/life/organizeLifeType")
public class OrganizeLifeTypeController extends BaseController {

	@Autowired
	private OrganizeLifeTypeService organizeLifeTypeService;
	
	@ModelAttribute
	public OrganizeLifeType get(@RequestParam(required=false) String id) {
		OrganizeLifeType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = organizeLifeTypeService.get(id);
		}
		if (entity == null){
			entity = new OrganizeLifeType();
		}
		return entity;
	}
	
	@RequiresPermissions("life:organizeLifeType:view")
	@RequestMapping(value = {"list", ""})
	public String list(OrganizeLifeType organizeLifeType, HttpServletRequest request, HttpServletResponse response, Model model) {
		organizeLifeType.setOrgId(UserUtils.getUser().getOffice().getId());
		Page<OrganizeLifeType> page = organizeLifeTypeService.findOrganizeTypeByOrgId(new Page<OrganizeLifeType>(request, response), organizeLifeType); 
		model.addAttribute("page", page);
		return "modules/life/organizeLifeTypeList";
	}

	@RequiresPermissions("life:organizeLifeType:view")
	@RequestMapping(value = "form")
	public String form(OrganizeLifeType organizeLifeType, Model model) {
		model.addAttribute("organizeLifeType", organizeLifeType);
		return "modules/life/organizeLifeTypeForm";
	}

	@RequiresPermissions("life:organizeLifeType:edit")
	@RequestMapping(value = "save")
	public String save(OrganizeLifeType organizeLifeType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, organizeLifeType)){
			return form(organizeLifeType, model);
		}
		organizeLifeTypeService.save(organizeLifeType);
		addMessage(redirectAttributes, "保存组织生活分类成功");
		return "redirect:"+Global.getAdminPath()+"/life/organizeLifeType/?repage";
	}
	
	@RequiresPermissions("life:organizeLifeType:edit")
	@RequestMapping(value = "delete")
	public String delete(OrganizeLifeType organizeLifeType, RedirectAttributes redirectAttributes) {
		organizeLifeTypeService.delete(organizeLifeType);
		addMessage(redirectAttributes, "删除组织生活分类成功");
		return "redirect:"+Global.getAdminPath()+"/life/organizeLifeType/?repage";
	}

}